using Bilibili.Common.Untils;
using Bilibili.Model.Resource;
using PCLUntils.Json;
using System.Web;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/creatempd", (string par) =>
{
    app.Run(async context =>
    {
        var dash = par.ParseObject<DashInfo>();
        dash.AudioUrl = HttpUtility.HtmlEncode(dash.AudioUrl);
        dash.VideoUrl = HttpUtility.HtmlEncode(dash.VideoUrl);
        var ts = "PT" + TimeSpan.FromMilliseconds(dash.DurationMS).ToString(@"hh\Hmm\Mss\S");
        var content = Dict.MPD_TEMPLATE
            .Replace("$Duration", ts)
            .Replace("$AudioID", dash.AudioID)
            .Replace("$AudioBandwidth", dash.AudioBandwidth)
            .Replace("$AudioCodec", dash.AudioCodec)
            .Replace("$Length", dash.Duration.ToString())
            .Replace("$AudioUrl", dash.AudioUrl)
            .Replace("$VideoID", dash.VideoID)
            .Replace("$VideoFrameRate", dash.VideoFrameRate)
            .Replace("$VideoBandwidth", dash.VideoBandwidth)
            .Replace("$VideoCodec", dash.VideoCodec)
            .Replace("$VideoWidth", dash.VideoWidth.ToString())
            .Replace("$VideoHeight", dash.VideoHeight.ToString())
            .Replace("$VideoUrl", dash.VideoUrl);
        context.Response.StatusCode = 200;
        context.Response.ContentType = "application/dash+xml";
        await context.Response.WriteAsync(content);
    });
});
app.Run();